package com.itheima.service.serviceimpl;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.WxOrderDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderMes;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private WxOrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public Order findById(Integer id) {
        return orderDao.findById(id);
    }

    @Transactional
    public Integer submitOrder(OrderMes order) {
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        String validateCode = (String) redisTemplate.opsForValue().get("ValidateCode");
        log.info("验证码:{}", order.getValidateCode());
        OrderSetting orderSetting = orderSettingDao.getOrderSettingByOrderDate(order.getOrderDate());
        Member member = memberDao.findMemberByIdCard(order.getIdCard());
        log.info("member:{}", member);

        //校验验证码是否正确或过期
        if (!order.getValidateCode().equals(validateCode)) {
            return -1;
        }
        //预约日期的预约人数没有设置的话不能预约。
        if (orderSetting == null) {
            return -2;
        }
        //预约日期的预约人数已经满了，不能预约。
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            return -3;
        }

        //当前用户不是会员，需要自动完成注册,提交预约信息，更新已预约人数
        if (member == null) {
            Member newMember = new Member();
            newMember.setRegTime(LocalDate.now());
            newMember.setIdCard(order.getIdCard());
            newMember.setName(order.getName());
            newMember.setSex(String.valueOf(order.getSex()));
            newMember.setPhoneNumber(order.getTelephone());
            memberDao.addMember(newMember);
            log.info("newMember:{}", newMember);
            order.setMemberId(newMember.getId());
            log.info("用户{}注册成功", newMember.getName());

        } else {
            order.setMemberId(member.getId());
            // 不能重复预约
            List<Order> orders = orderDao.findByMemberId(member.getId());
            for (Order order1 : orders) {
                if (order1.getOrderDate().equals(order.getOrderDate()) && order1.getSetmealId() == order.getSetmealId()) {
                    return -4;
                }
            }
        }

        //更新已预约人数
        orderSettingDao.editReservationsrByOrderDate(orderSetting);
        orderDao.submitOrder(order);
        log.info("更新预约人数成功:{}", order);
        return order.getId();

    }
}
